package cn.edu.guet.service.editor.impl;

import cn.edu.guet.bean.editor.Badge;
import cn.edu.guet.bean.editor.EditorUser;
import cn.edu.guet.bean.editor.Option;
import cn.edu.guet.bean.editor.Post;
import cn.edu.guet.service.editor.PostBll;
import cn.edu.guet.mapper.editor.PostMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * create by hzg 2021/07/07
 * 帖子的各个业务实现类
 */
@Service
public class PostBllImpl implements PostBll {

    @Autowired
    private Post postBean;

    @Autowired
    private PostMapper postMapper;

    @Override
    public Map uploadImg(MultipartFile file) {
        if (file.isEmpty()) {
            System.out.println("文件为空");
            return null;
        }
        //获取target目录的路径
        Resource resource = new ClassPathResource("");
        String realpath = "";
        try{
            realpath = resource.getFile().getAbsolutePath();
        }catch (IOException e){
            e.printStackTrace();
        }
        String fileName = file.getOriginalFilename();  // 文件名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));  // 后缀名
        String filePath = realpath+"\\static\\upload\\"; // 上传后的路径
        fileName = UUID.randomUUID() + suffixName; // 新文件名
        File dest = new File(filePath + fileName);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //给前端返回数据
        String filename = "../../upload/" + fileName;
        Map<String,Object> map = new HashMap<>();
        map.put("errno",0);
        List<Map> list = new ArrayList<>();
        Map<String,String> remap = new HashMap<>();
        remap.put("url",filename);
        list.add(remap);
        map.put("data",list);
        return map;
    }

    @Override
    public String publishPost(String tid, String title, String content,String time,String pstatus,String userid) {
        Post postBean = new Post();
        postBean.setPtitle(title);
        postBean.setPtime(time);
        postBean.setTid(tid);
        postBean.setPcontent(content);
        postBean.setPstatus(pstatus);
        postBean.setUserid(userid);
        postBean.setGiveup(0);//新的帖子点赞数量为0
        postBean.setCollect(0);//新的帖子收藏数量为0
        postMapper.insertPost(postBean);
        return postBean.getPid();
    }

    @Override
    public Post selectPostById(String pid) {
        postBean = postMapper.selectPostById(pid);
        String userid = postBean.getUserid();
        String gname = postMapper.selectGnameById(userid);
        postBean.setGname(gname);
        return postBean;
    }

    @Override
    public EditorUser selectUserById(String userid) {
        return postMapper.selectUserById(userid);
    }

    @Override
    public void giveupPost(String pid,String userid,String gdate) {
        //在点赞表中加入记录
        postMapper.insertGiveup(pid,userid,gdate);
        //先查询当前点赞数量,在点赞数量的基础上加一
        int con = postMapper.selectPostGiveup(pid)+1;
        //更新点赞数量
        postMapper.updatePostGiveup(con,pid);
    }

    @Override
    public void deleteGiveup(String pid, String userid) {
        //在点赞表中删除记录
        postMapper.deleteGiveup(pid,userid);
        //查询数量并减一
        int con = postMapper.selectPostGiveup(pid)-1;
        //在帖子表中更新数量
        postMapper.updatePostGiveup(con,pid);
    }

    @Override
    public void updateGiveupPoint(String useredid, String userid,String pid) {
        //在点赞表中查询点赞记录的日期
        String ldate = postMapper.selectGiveupDate(userid,pid);
        //积分处理
        insertGradeByPost(useredid, -1, ldate);
    }

    @Override
    public int selectGiveupCon(String pid) {
        int con =Integer.valueOf(postMapper.selectPostGiveup(pid));
        return con;
    }

    @Override
    public void collectPost(String userid, String pid,String cdate) {
        //在收藏表中加入记录
        postMapper.collectPost(userid,pid,cdate);
        //查询当前收藏数量，在此基础上加一
        int con = postMapper.selectPostCollection(pid)+1;
        //更新收藏数量
        postMapper.updatePostCollection(con,pid);
    }

    @Override
    public void deleteCollection(String userid, String pid) {
        //在点赞表中删除记录
        postMapper.deleteCollection(userid,pid);
        //查询当前收藏数量，在此基础上减一
        int con = postMapper.selectPostCollection(pid)-1;
        //更新收藏数量
        postMapper.updatePostCollection(con,pid);
    }

    @Override
    public void updateCollectPoint(String useredid, String userid, String pid) {
        //在收藏表中查询点赞记录的日期
        String ldate = postMapper.selectCollectDate(userid,pid);
        //积分处理
        insertGradeByPost(useredid, -2, ldate);
    }

    @Override
    public int selectCollectCon(String pid) {
        int con = postMapper.selectPostCollection(pid);
        return con;
    }

    @Override
    public void updatePost(String pid,String tid,String ptitle,String pcontent,String ptime,String pstatus) {
        postBean.setPid(pid);
        postBean.setTid(tid);
        postBean.setPtitle(ptitle);
        postBean.setPcontent(pcontent);
        postBean.setPtime(ptime);
        postBean.setPstatus(pstatus);
        postMapper.updatePost(postBean);
    }

    @Override
    public void deletePost(String pid) {
        postMapper.deletePost(pid);
    }

    @Override
    public String selectGiveupOrNot(String pid, String userid) {
        return postMapper.selectGiveupOrNot(pid,userid);
    }

    @Override
    public String selectCollectOrNot(String pid, String userid) { return postMapper.selectCollectOrNot(pid,userid); }

    @Override
    public void insertfatherDiscuss(int pid, int userid, String comcontent, String comtime, int comup, int fatherid,int useredid) {
        postMapper.insertfatherDiscuss(pid,userid,comcontent,comtime,comup,fatherid,useredid);
    }

    @Override
    public List<Option> selectSelect() {
        List<Option> list = postMapper.selectSelect();
        return list;
    }

    @Override
    public String selectUseridByPid(String pid) { return postMapper.selectUseridByPid(pid); }

    @Override
    public void insertGradeByPost(String userid,int grade,String ldate) {
        //查询用户当天是否有得分记录
        String lgrade = postMapper.selectUserPointByDay(userid,ldate);//用String是为了好判空
        if (lgrade!=null){//如果有则取出并加grade
            if (Integer.valueOf(lgrade)>=30 && grade>0){//如果为30并且是加积分那就直接return
                return;
            }
            //g的值上限为30
            int g = Integer.valueOf(lgrade)+grade>30?30:Integer.valueOf(lgrade)+grade;
            int a3 = postMapper.updateGradeOnUP(userid,ldate,g);
            System.out.println("PostBllImpl-insertGradeByPost-a3="+a3);
        }else { //如果没有则新增一条记录 0代表未签到 1代表签到
            postMapper.insertGradeOnUP( Integer.valueOf(userid), grade, ldate, "0");
        }

        //查询用户总积分
        String totalpoint = postMapper.selectUserPoint(userid);//用String是为了好判空
        if(totalpoint!=null){//如果积分不为空，则在总积分的基础上加grade
            int g = Integer.valueOf(lgrade)+grade;
            int p;int a1;
            if (g<30) {
                p = Integer.valueOf(totalpoint) + grade;
            }else{
                //限制每日加积分最大是30
                int h = grade-(g-30);
                p = Integer.valueOf(totalpoint)+h;
            }
            a1 = postMapper.updateGradeOnUser(userid, p);
            System.out.println("PostBllImpl-insertGradeByPost-a1=" + a1);
        }else{//如果积分为空则在表中加入积分
            int a2 = postMapper.updateGradeOnUser(userid,grade);
            System.out.println("PostBllImpl-insertGradeByPost-a2="+a2);
        }
    }

    @Override
    public  List<Badge> selectBadgeByUser(String userid) {
        return postMapper.selectBadgeByUser(userid);
    }

    @Override
    public Post selectPostSDByPid(String pid) { return postMapper.selectPostSDByPid(pid); }

    @Override
    public String selectUserRole(String userid) {
        return postMapper.selectUserRole(userid);
    }

    @Override
    public String selectPointByDay(String userid, String date) {
        String point = postMapper.selectUserPointByDay(userid,date);
        int point1 = 0;
        if (point!=null){
            point1 = Integer.valueOf(point);
            int con = 30-point1;
            if (con>=10){
                return "10";
            }else{
                return con+"";
            }
        }
        return 10+"";
    }
}
